﻿function view_onOpen()
{
}
function CheckPAYE() 
{
	var tax=0;
	var taxable=tax0.value;
	var wmpaidper = "M";  
 	var over65 = "N"; 

	if (radio2.value==true)
	{
		wmpaidper="W";
	}
	if (radio3.value==true)
	{
		wmpaidper="F";
	}
	if (radio5.value==true)
	{
		over65="Y";
	}
	if (radio7.value==true)
	{
		over65="S";
	}
	
	var rates = new Array([0,0.18],[150000,0.25],[235000,0.30],[325000,0.35],[455000,0.38],[580000,0.40]);
	var PrmReb=10755; 
	var o65Reb=6012;
	var o75Reb=2000;
	var th1=59750;
	var th2=93150;
	var th3=104261;

	if(wmpaidper=='W')
	{
		taxable=taxable*52;
	}
	else if(wmpaidper=='F')
	{
		taxable=taxable*52/2;
	}
	else if(wmpaidper=='M')
	{
		taxable=taxable*12;
	}

	if(over65=='Y')
	{
		if(taxable<th2)
		{
			tax=0;
		}
		else
		{
			for(t=5;t>=0;t--)
			{
				if(taxable>rates[t][0])
				{
					tax=tax+(taxable-rates[t][0])*rates[t][1];
					taxable=rates[t][0];
				}
			}

			tax=tax-PrmReb-o65Reb;

			if(wmpaidper=='W'){tax=tax/52;}
			else if(wmpaidper=='F'){tax=(tax*2)/52;}
			else if(wmpaidper=='M'){tax=tax/12;}
		}
	}
	else if(over65=='S')
	{
		if(taxable<th3)
		{
			tax=0;
		}
		else
		{
			for(t=5;t>=0;t--)
			{
				if(taxable>rates[t][0])
				{
					tax=tax+(taxable-rates[t][0])*rates[t][1];
					taxable=rates[t][0];
				}
			}

			tax=tax-PrmReb-o65Reb-o75Reb;

			if(wmpaidper=='W'){tax=tax/52;}
			else if(wmpaidper=='F'){tax=(tax*2)/52;}
			else if(wmpaidper=='M'){tax=tax/12;}
		}
	}
	else if(over65=='N')
	{
		if(taxable<th1)
		{
			tax=0;
		}
		else
		{
			for(t=5;t>=0;t--)
			{
				if(taxable>rates[t][0])
				{
					tax=tax+(taxable-rates[t][0])*rates[t][1];
					taxable=rates[t][0];

				}
			}

			tax=tax-PrmReb;

			if(wmpaidper=='W'){tax=tax/52;}
			else if(wmpaidper=='F'){tax=(tax*2)/52;}
			else if(wmpaidper=='M'){tax=tax/12;}
		}
	}
	tax=formatFloat(tax, 2);
	paye.innerText='PAYE: '+tax;
}

function formatFloat(src, pos)
{
    return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
}